/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package aviso.entity;

import java.io.Serializable;
import java.util.Calendar;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;

/**
 *
 * @author avgusti
 */
@Entity
@NamedQueries({
    @NamedQuery(name = "Advert.lookUpInBounds", query = "select a from Advert a where "
    + "a.lat>=:lats and a.lat<=:latn and "
    + "a.lng>=:lngw and a.lng<=:lnge order by abs(a.lat-:latm) asc, abs(a.lng-:lngm) asc, a.address asc"),
    @NamedQuery(name = "Advert.listByUser", query = "select a from Advert a where "
    + "a.user.id=:userid"),
    @NamedQuery(name = "Advert.listUserHestory", query = "select u.history from UserName u where u.id=:userid")
})
// @NamedQuery(name = "Advert.lookUp", query = "select a from Advert a where "
//    + "(a.lat>=:latw AND a.lat<=:late AND"
//    + "a.lng>=:lngs AND a.lng<=:lngn) AND"
//    + "(a.type:=:type OR 0=:type) AND"
//    + "(a.floor >=:minfloor AND a.floor<=:maxfloor) AND"
//    + ""
//    + "")
public class Advert extends ListWraper implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    @NotNull
    private String address;
    private Double lat, lng;
    @NotNull
    private AdvType type = AdvType.APPARTMENT;
    private int floors, floor;
    private int total_sqr, kitchen_sqr, living_sqr, balkony_sqr;
    private int room_count;
    private int price;
    @Temporal(TemporalType.TIMESTAMP)
    private Calendar added_Date;
    @Temporal(TemporalType.DATE)
    private Calendar expire_Date;
    @Temporal(TemporalType.DATE)
    private Calendar expire_adv_Date;
    private boolean advertised;
    private String contact;
    private String notes;
    @ManyToMany(fetch = FetchType.EAGER)
    private List<ImageFile> files;
    @ManyToOne
    private UserName user;

    public Advert() {
    }

    public Advert(Long id, Double lat, Double lng) {
        this.id = id;
        this.lat = lat;
        this.lng = lng;
    }

    public List<ImageFile> getFiles() {
        return files;
    }

    public void setFiles(List<ImageFile> files) {
        this.files = files;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Calendar getAdded_Date() {
        return added_Date;
    }

    public void setAdded_Date(Calendar added_Date) {
        this.added_Date = added_Date;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public boolean isAdvertised() {
        return advertised;
    }

    public void setAdvertised(boolean advertised) {
        this.advertised = advertised;
    }

    public int getBalkony_sqr() {
        return balkony_sqr;
    }

    public void setBalkony_sqr(int balkony_sqr) {
        this.balkony_sqr = balkony_sqr;
    }

    public String getContact() {
        return contact;
    }

    public void setContact(String contact) {
        this.contact = contact;
    }

    public Calendar getExpire_Date() {
        return expire_Date;
    }

    public void setExpire_Date(Calendar expire_Date) {
        this.expire_Date = expire_Date;
    }

    public Calendar getExpire_adv_Date() {
        return expire_adv_Date;
    }

    public void setExpire_adv_Date(Calendar expire_adv_Date) {
        this.expire_adv_Date = expire_adv_Date;
    }

    public int getFloor() {
        return floor;
    }

    public void setFloor(int floor) {
        this.floor = floor;
    }

    public int getFloors() {
        return floors;
    }

    public void setFloors(int floors) {
        this.floors = floors;
    }

    public int getKitchen_sqr() {
        return kitchen_sqr;
    }

    public void setKitchen_sqr(int kitchen_sqr) {
        this.kitchen_sqr = kitchen_sqr;
    }

    public Double getLat() {
        return lat;
    }

    public void setLat(Double lat) {
        this.lat = lat;
    }

    public int getLiving_sqr() {
        return living_sqr;
    }

    public void setLiving_sqr(int living_sqr) {
        this.living_sqr = living_sqr;
    }

    public Double getLng() {
        return lng;
    }

    public void setLng(Double lng) {
        this.lng = lng;
    }

    public String getNotes() {
        return notes;
    }

    public void setNotes(String notes) {
        this.notes = notes;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public int getRoom_count() {
        return room_count;
    }

    public void setRoom_count(int room_count) {
        this.room_count = room_count;
    }

    public int getTotal_sqr() {
        return total_sqr;
    }

    public void setTotal_sqr(int total_sqr) {
        this.total_sqr = total_sqr;
    }

    public AdvType getType() {
        return type;
    }

    public void setType(AdvType type) {
        this.type = type;
    }

    public UserName getUser() {
        return user;
    }

    public void setUser(UserName user) {
        this.user = user;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Advert)) {
            return false;
        }
        Advert other = (Advert) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "aviso.entity.Advert[ id=" + id + " ]";
    }

    public String advertImage() {
        if (files != null && files.size() > 0 && user != null && user.getId() != null) {
            return user.getName() + "\\" + files.get(0).getName();
        }
        return "blank.png";
    }
}
